package cn.tworice.client.web;

import cn.tworice.client.service.ExamService;
import cn.tworice.common.entity.Exam;
import cn.tworice.common.entity.Paper;
import cn.tworice.common.entity.dto.ExamDto;
import com.alibaba.fastjson.JSONArray;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试
 * @Author 二饭
 * @Email 1786705138@qq.com
 * @Date 2021/12/25 20:22
 */
@RestController
@RequestMapping("client/exam")
@CrossOrigin
public class ExamClient {
    @Autowired
    private ExamService examService;

    @GetMapping("")
    public RequestResult list(Integer page, Integer pageSize) {
        RequestResult result = new RequestResult();
        List<ExamDto> list = examService.getExamList(page, pageSize);
        Integer total = examService.getExamTotal();
        result.appendData("list", list);
        result.appendData("total", total);
        return result;
    }

    @PutMapping("")
    public RequestResult set(Exam exam) {
        RequestResult result = new RequestResult();
        examService.setExam(exam);
        return result;
    }

    @DeleteMapping("")
    public RequestResult del(String[] id) {
        RequestResult result = new RequestResult();
        examService.delExamByIds(id);
        result.setCode(new StatusCode(200,"删除成功"));
        return result;
    }

    /**
     * 提交试卷
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/25 20:23
     */
    @PutMapping("paper")
    public RequestResult submitPaper(String papers){
        RequestResult result = new RequestResult();
        List<Paper> list = JSONArray.parseArray(papers, Paper.class);
        examService.setPaper(list);
        return result;
    }
}
